MY_DIR =$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
PROJECT_ROOT=$(shell dirname $(MY_DIR))
BUILD_ROOT=$(PROJECT_ROOT)/build
PROTOC=$(BUILD_ROOT)/bin/protoc
PROTO_INC=$(MY_DIR)/include

TARGET=../output/bin/http-gateway

PROTOS_PATH = $(PROJECT_ROOT)/powermaster/powermaster-proto/src/main/proto:$(PROJECT_ROOT)/market/market-proto/src/main/proto
vpath %.proto $(PROTOS_PATH)

all: $(TARGET)

powermaster: powermaster.proto
	$(PROTOC) -I$(PROTO_INC) --go_out=plugins=grpc:./src -I$(PROTOS_PATH) powermaster.proto poweragent.proto
	$(PROTOC) -I$(PROTO_INC) --grpc-gateway_out=logtostderr=true,allow_delete_body=true:./src -I$(PROTOS_PATH) powermaster.proto poweragent.proto

powermarket: market.proto
	$(PROTOC) -I$(PROTO_INC) --go_out=plugins=grpc:./src -I$(PROTOS_PATH) market.proto
	$(PROTOC) -I$(PROTO_INC) --grpc-gateway_out=logtostderr=true:./src -I$(PROTOS_PATH) market.proto

market:

$(TARGET): powermaster powermarket
	@mkdir -p $(PROJECT_ROOT)/output/bin/fe
	go build -o $(TARGET) src/http-gateway/http-gateway.go src/http-gateway/secure.go
	if [ ! -d "$(shell dirname $(PROJECT_ROOT))/fe/" ]; then mkdir -p $(shell dirname $(PROJECT_ROOT))/fe/; fi
	@cp ./conf/* ../output/conf/
	@cp -r $(PROJECT_ROOT)/ui/devel/* $(shell dirname $(PROJECT_ROOT))/fe/
	cd $(shell dirname $(PROJECT_ROOT))/fe;npm install;npm run build
	@cp -r $(shell dirname $(PROJECT_ROOT))/fe/dist $(PROJECT_ROOT)/ui/devel/
	@cp -r $(PROJECT_ROOT)/ui/devel/dist/* $(PROJECT_ROOT)/output/bin/fe

.PHONY: all
